package com.sinoglobal.ningxia.activity.interestingGame;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.shop.PurchaseGoodsActivity;
import com.sinoglobal.ningxia.adapter.MyPrizeAdapter;
import com.sinoglobal.ningxia.beans.MyPrizeInfoVo;
import com.sinoglobal.ningxia.beans.MyPrizeVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.AbstractActivity;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.Code;

/**
 * 
 * @author zbl
 * @createdate 2014-7-28 下午1:14:34
 * @Description:积分转盘“”我的
 */
public class MyPrizeActivity extends AbstractActivity {
	private ListView myPrize;
	private TextView service_tel;
	private MyPrizeAdapter myPrizeAdapter;
	private ArrayList<MyPrizeInfoVo> myPrizeInfoVos;
	private String kefu;
	private Intent intent = null;
	private RelativeLayout nogoods;
	private int position_choice = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_prize);
		titleView.setText(getString(R.string.mycoupons));
		init();
		addListener();
		loadInfo();
	}

	private void loadInfo() {
		new ItktAsyncTaskWithDialog<Void, Void, MyPrizeVo>(getString(R.string.get_listing), true, true) {

			@Override
			public MyPrizeVo before(Void... params) throws Exception {
				return RemoteImpl.getInstance().getMyPrizeInfo();
			}

			@Override
			public void after(MyPrizeVo result) {
				if (result != null) {
					if (Code.CODE_SUCCESS == result.getCode()) {
						myPrizeInfoVos = result.getJson();
						myPrizeAdapter = new MyPrizeAdapter(myPrizeInfoVos, MyPrizeActivity.this);
						myPrize.setAdapter(myPrizeAdapter);
						// kefu = result.getKefu();
						// service_tel.setText(kefu);
					} else if (2 == result.getCode()) {
						nogoods.setVisibility(View.VISIBLE);
						myPrize.setVisibility(View.GONE);
					} else {
						Toast.makeText(FlyApplication.context, result.getMessage(), Toast.LENGTH_SHORT).show();
					}
				}
			}

			@Override
			public void exception() {

			}

		}.execute();

	}

	private void addListener() {
		myPrize.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// 先判断奖品是否领取
				// 若已领取货已返货
				if (!myPrizeInfoVos.get(position).getPrize_state().equals("2")) {
					// 跳转到领奖信息页
					intent = new Intent(MyPrizeActivity.this, AcceptPrizeActivity.class);
					intent.putExtra("id", myPrizeInfoVos.get(position).getId());
					startActivity(intent);
				} else {
					position_choice = position;
					// 跳转到填写信息的详情页
					intent = new Intent(MyPrizeActivity.this, PurchaseGoodsActivity.class);
					intent.putExtra("type", "3");
					intent.putExtra("goods_id", myPrizeInfoVos.get(position).getId());
					startActivityForResult(intent, 0);
				}
			}

		});

	}

	private void init() {
		myPrize = (ListView) findViewById(R.id.myPrize);
		service_tel = (TextView) findViewById(R.id.service_tel);
		nogoods = (RelativeLayout) findViewById(R.id.nogoods);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		//Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null) {
			if (data.getBooleanExtra("success", false)) {
				myPrizeAdapter.getMyPrizeVo().get(position_choice).setPrize_state("1");
				myPrizeAdapter.notifyDataSetChanged();
				position_choice = -1;
			}

		}
	}

}
